//
//  HighQualityStoreViewModel.m
//  GLGW_Shoping
//
//  Created by yang on 2018/12/19.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "HighQualityStoreViewModel.h"
#import "SearchStoreTableViewCell.h"
#import "HightStoreTopView.h"
#import "StoreTopSelectView.h"
#import "ComprehensiveTypeView.h"
#import "TableViewPleaseholdView.h"

@interface HighQualityStoreViewModel ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView * superTableView;
    NSMutableArray * storeDataSource;
}
@end

@implementation HighQualityStoreViewModel

-(instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        storeDataSource = [NSMutableArray arrayWithCapacity:0];
        tableView.y = tableView.y + kHeightProportion_In_iPhoneTriploid(44);
        tableView.height = tableView.height - kHeightProportion_In_iPhoneTriploid(44);
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.rowHeight = kHeightProportion_In_iPhoneTriploid(126);
        [superView addSubview:tableView];
        superTableView = tableView;
        [self createTopView:superView];
        [self requestStoreData];
    }
    return self;
}

-(void)createTopView:(UIView *)t_View{
    HightStoreTopView * searchTopView = [[HightStoreTopView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(44)) inSuperView:t_View];
    searchTopView.Block_SearchStr = ^(NSString * _Nonnull s_Str) {
        DLog(@"%@",s_Str);//
    };
//    kSearchshop
    //
//    if (self.accountType == AccountType_Ordinary) {
//        StoreTopSelectView * st_view = [[StoreTopSelectView alloc] initWithFrame:CGRectMake(0, kY_With_View(searchTopView, 0), KScreenWidth, kHeightProportion_In_iPhoneTriploid(34)) inSuperView:t_View];
//        st_view.backgroundColor = [UIColor whiteColor];
//    }else{
//        ComprehensiveTypeView * comprehensiveView = [[ComprehensiveTypeView alloc] initWithFrame:CGRectMake(0, kY_With_View(searchTopView, 0), KScreenWidth, kHeightProportion_In_iPhoneTriploid(34)) inSuperView:t_View];
//    }
}

-(void)noDataShowView{
    if (storeDataSource.count == 0) {
        TableViewPleaseholdView * nodata_View = [[TableViewPleaseholdView alloc] initWithFrame:CGRectMake(0, kHeightProportion_In_iPhoneTriploid(130), KScreenWidth, kHeightProportion_In_iPhoneTriploid(200)) thePicStr:@"icon_quesehng_shangjiaeuzhu" theTosStr:@"商家正在入驻，敬请期待～" theMiaoStr:@"" theButStr:@"" theButWidth:0];
        [self showNoDataImageView:nodata_View];
    }
}

#pragma mark -- 数据请求
-(void)requestStoreData{
    @weakify(self)
    _store_recommendCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        @strongify(self)
        RACSignal * store_Signal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            NSDictionary * storeDic = @{@"page":@([input integerValue]),@"pageSize":@15};
            if ([input integerValue] == 1) {
                [self->storeDataSource removeAllObjects];
            }
            NSString * apiStr;
            if (self.accountType == AccountType_Ordinary) {
                apiStr = kStoreInfoList;
            }else{
                apiStr = kFreeStoreInfoList;
            }
            [GLGWRequestTool POST:[NSString stringWithFormat:@"%@%@",kApiPrefix,apiStr] lodingView:nil lodingTextString:@"" isLoding:YES parameters:storeDic success:^(id  _Nullable response) {
                [subscriber sendNext:response];
                [subscriber sendCompleted];
            } failure:^(NSError * _Nullable error) {
                [self noDataShowView];
                [subscriber sendNext:error];
                [subscriber sendCompleted];
            }];
            return nil;
        }];
        
        return [store_Signal map:^id _Nullable(id  _Nullable value) {
            HightQualityStoreModel * hightQualityModel = [HightQualityStoreModel yy_modelWithJSON:value];
            if (hightQualityModel.code == 0) {
                [self->storeDataSource addObjectsFromArray:hightQualityModel.data.infos];
                [self noDataShowView];
                [self->superTableView reloadData];
            }
            return hightQualityModel;
        }];
    }];

}
#pragma mark -- UITableViewDataSource,UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return storeDataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HightQualityStoreModel_Infos * i_Model = storeDataSource[indexPath.row];
    SearchStoreTableViewCell * cell = [SearchStoreTableViewCell cellWithTableView:tableView];
    cell.hightStoreModel = i_Model;
    cell.Block_PushStore = ^{
        if (self.delegate && [self.delegate respondsToSelector:@selector(stroreInformation:)]) {
            [self.delegate stroreInformation:i_Model];
        }
    };
    return cell;
}
@end
